#include "stdafx.h"
#include "EventManager.h"
#include "Event.h"
#include "View.h"
#include "WidgetWin.h"
#include "FocusManager.h"
EventManager::EventManager()
	: m_functionKey(0)
{
}


EventManager::~EventManager()
{
}

bool EventManager::onMouseMove(short x, short y)
{
	MouseEvent mouseEvent;
	mouseEvent.type = TYPE_TOUCH_MOVE;
	mouseEvent.functionKey = m_functionKey;
	mouseEvent.tickcount = ::GetTickCount();
	mouseEvent.x = x;
	mouseEvent.y = y;

	View* focusView = WidgetWin::getInstance().getFocusManager()->getFocusView();

	if (focusView != NULL)
	{
		if (focusView->processMouseEvent(mouseEvent))
		{
			return true;
		}
	}

	return false;
}

bool EventManager::onLButtonDown(short x, short y)
{
	m_functionKey |= FUNCTIONKEY_LBTN;

	MouseEvent mouseEvent;
	mouseEvent.type = TYPE_TOUCH_CLICK;
	mouseEvent.functionKey = m_functionKey;
	mouseEvent.tickcount = ::GetTickCount();
	mouseEvent.x = x;
	mouseEvent.y = y;

	View* focusView = WidgetWin::getInstance().getFocusManager()->getFocusView();
	if (focusView != NULL)
	{
		if (focusView->processMouseEvent(mouseEvent))
		{
			return true;
		}
	}

	return false;
}

bool EventManager::onLButtonDBClick(short x, short y)
{
	MouseEvent mouseEvent;
	mouseEvent.type = TYPE_TOUCH_DCLICK;
	mouseEvent.functionKey = m_functionKey;
	mouseEvent.tickcount = ::GetTickCount();
	mouseEvent.x = x;
	mouseEvent.y = y;

	View* focusView = WidgetWin::getInstance().getFocusManager()->getFocusView();
	if (focusView != NULL)
	{
		if (focusView->processMouseEvent(mouseEvent))
		{
			return true;
		}
	}

	return false;
}

bool EventManager::onLButtonUp(short x, short y)
{
	m_functionKey &= ~FUNCTIONKEY_LBTN;

	MouseEvent mouseEvent;
	mouseEvent.type = TYPE_TOUCH_CLICKUP;
	mouseEvent.functionKey = m_functionKey;
	mouseEvent.tickcount = ::GetTickCount();
	mouseEvent.x = x;
	mouseEvent.y = y;

	if (m_mouseEvents.size() > 0)
	{
		MouseEvent event = m_mouseEvents.front();
		if (event.type = TYPE_TOUCH_LONGPRESS)
		{
			mouseEvent.type = TYPE_TOUCH_LONGPRESSUP;
		}
	}

	View* focusView = WidgetWin::getInstance().getFocusManager()->getFocusView();
	if (focusView != NULL)
	{
		if (focusView->processMouseEvent(mouseEvent))
		{
			return true;
		}
	}

	return false;
}


bool EventManager::onChar(uint32 code)
{
	KeyEvent keyEvent;
	keyEvent.key_code = code;
	keyEvent.functionKey = m_functionKey;
	keyEvent.tickcount = ::GetTickCount();

	View* focusView = WidgetWin::getInstance().getFocusManager()->getFocusView();

	if (focusView != NULL)
	{
		if (focusView->processKeyEvent(keyEvent))
		{
			return true;
		}
	}

	return false;
}


bool EventManager::onKeyDown(uint32 code)
{
	KeyEvent keyEvent;
	keyEvent.key_code = code;
	keyEvent.functionKey = m_functionKey;
	keyEvent.tickcount = ::GetTickCount();

	View* focusView = WidgetWin::getInstance().getFocusManager()->getFocusView();

	if (focusView != NULL)
	{
		if (focusView->processRawKeyEvent(keyEvent))
		{
			return true;
		}
	}

	return false;
}

bool EventManager::onMouseWheel(short value, short x, short y)
{
	View* focusView = WidgetWin::getInstance().getFocusManager()->getFocusView();
	if (focusView)
	{
		MouseEvent event;
		event.functionKey = m_functionKey;
		event.tickcount = GetTickCount();
		event.type = TYPE_TOUCH_WHEEL;
		event.x = x;
		event.y = y;
		event.param = value;
		return focusView->processMouseEvent(event);
	}

	return false;
}